package com.qjc.config;

import com.qjc.constant.I18nConstant;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * 国际化语言解析器，如果未设置则默认中文
 *
 * @ClassName: MyLocaleResolver
 * @Author: qjc
 * @Date: 2022/12/28 13:53
 */
public class MyLocaleResolver implements LocaleResolver {

    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        //获取前端传来的数据
        String language = httpServletRequest.getHeader(I18nConstant.LANGUAGE_KEY);
        Locale locale = Locale.getDefault();
        if (!StringUtils.isEmpty(language)) {
            locale = new Locale(language);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}